package com.wing.cabinet.controller.admin;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.cabinet.model.entity.CutterLifeCycle;
import com.wing.cabinet.model.request.CutterLifeCycleForm;
import com.wing.cabinet.model.response.CutterLifeCycleVO;
import com.wing.cabinet.service.CutterLifeCycleService;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.web.annotation.RequestObjectFieldIgnore;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.lang.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author
 */
@Validated
@Api(value = "Admin-刀具生命周期管理", tags = "#/cabinet/cutterLifeCycle")
@RestController("cutterLifeCycleController")
@RequestMapping("cabinet/admin/cutterLifeCycle")
@RequiredArgsConstructor
public class CutterLifeCycleController {

    private final CutterLifeCycleService cutterLifeCycleService;


    @ApiOperation(value = "查询所有刀具生命周期", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "packLabelCode", value="刀具条码", defaultValue = "", example = "", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "skuCode", value="物料编号", defaultValue = "", example = "", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "skuName", value="物料名称", defaultValue = "", example = "", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "specCode", value="规格码", defaultValue = "", example = "", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "usherInId", value="领新人ID", defaultValue = "", example = "", dataType = "Long", paramType = "query")
            ,@ApiImplicitParam(name = "status", value="状态", defaultValue = "", example = "", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "curOperatorId", value="当前操作人ID", defaultValue = "", example = "", dataType = "Long", paramType = "query")
            ,@ApiImplicitParam(name = "isCheck", value="是否盘验", defaultValue = "", example = "", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "inspectorId", value="盘验人ID", defaultValue = "", example = "", dataType = "Long", paramType = "query")
    })
    @GetMapping("/list")
    public PageResult<CutterLifeCycleVO> list(
            @RequestParam(defaultValue = "1") Integer page
            ,@RequestParam(defaultValue = "10") Integer limit
            ,@RequestParam(defaultValue = "id") String sort
            ,@RequestParam(defaultValue = "desc") String order
            ,String packLabelCode
            ,String skuCode
            ,String skuName
            ,String specCode
            ,Long usherInId
            ,CutterLifeCycle.Status status
            ,Long curOperatorId
            ,Integer isCheck
            ,Long inspectorId
            ,Integer isReturn
    ){
        Page<CutterLifeCycle> pg = new Page<>(page, limit);
        Map queryParam=new HashMap();
        queryParam.put("sort",StrUtil.toUnderlineCase(sort));
        queryParam.put("order",order);
        // 搜索
        if(StringUtils.isNotBlank(packLabelCode)){
            queryParam.put("packLabelCode", packLabelCode);
        }
        if(StringUtils.isNotBlank(skuCode)){
            queryParam.put("skuCode", skuCode);
        }
        if(StringUtils.isNotBlank(skuName)){
            queryParam.put("skuName", skuName);
        }
        if(StringUtils.isNotBlank(specCode)){
            queryParam.put("specCode", specCode);
        }
        if(null != usherInId){
            queryParam.put("usherInId", usherInId);
        }
        if(null != status){
            queryParam.put("status", status.getValue());
        }
        if(null != curOperatorId){
            queryParam.put("curOperatorId", curOperatorId);
        }
        if(null != isCheck){
            queryParam.put("isCheck", isCheck);
        }
        if(null != inspectorId){
            queryParam.put("inspectorId", inspectorId);
        }
        if(null != isReturn){
            queryParam.put("isReturn", isReturn);
        }
        IPage<CutterLifeCycleVO> list = cutterLifeCycleService.pageList4app(pg,queryParam);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }



    @ApiOperation(value = "添加刀具生命周期", notes = "添加")
    @RequestObjectFieldIgnore({"id", "createTime", "remark"})
    @PostMapping("/add")
    public JsonResult add(@Validated(CutterLifeCycleForm.Add.class) CutterLifeCycleForm cutterLifeCycleForm) {
        if (cutterLifeCycleService.add(cutterLifeCycleForm) > 0) {
            return JsonResult.success("添加成功");
        }
        return JsonResult.fail("添加失败");
    }




    @ApiOperation(value = "修改刀具生命周期", notes = "修改")
    @PutMapping("/update")
    public JsonResult update(@Validated(CutterLifeCycleForm.Update.class) CutterLifeCycleForm cutterLifeCycleForm) {
        CutterLifeCycle cutterLifeCycle = cutterLifeCycleService.getById(cutterLifeCycleForm.getId());
        if(cutterLifeCycle == null){
        return JsonResult.fail("修改失败！");
        }
        CopyOptions copyOptions = CopyOptions.create()
        .setEditable(CutterLifeCycle.class)
        .setIgnoreError(true)
        .setIgnoreNullValue(true);
        BeanUtil.copyProperties(cutterLifeCycleForm, cutterLifeCycle, copyOptions);
        if (cutterLifeCycleService.updateById(cutterLifeCycle)) {
            return JsonResult.success("修改成功！");
        }
        return JsonResult.fail("修改失败！");
    }



    @ApiOperation(value = "删除刀具生命周期", notes = "删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "cutterLifeCycleId", value = "刀具生命周期id", required = true, dataType = "Integer", paramType = "path")
    })
    @DeleteMapping("/{id}" )
    public JsonResult delete(@PathVariable("id") Long cutterLifeCycleId) {
        if (cutterLifeCycleService.removeById(cutterLifeCycleId)) {
            return JsonResult.success("删除成功");
        }
        return JsonResult.fail("删除失败");
    }


    @ApiOperation(value = "状态列表")
    @GetMapping("/statusList")
    public JsonResult statusList() {
        List<Map<String, String>> maps = new ArrayList<>();
        for (CutterLifeCycle.Status status : CutterLifeCycle.Status.values()) {
            if(!status.equals(CutterLifeCycle.Status.getFresh)
                    && !status.equals(CutterLifeCycle.Status.takeOut)
                    && !status.equals(CutterLifeCycle.Status.tradeIn)
                    && !status.equals(CutterLifeCycle.Status.check)){
                Map<String, String> map = new HashedMap();
                map.put("status", status.toString());
                map.put("desc", status.getDesc());
                maps.add(map);
            }
        }
        return JsonResult.success("操作成功", maps);
    }

}
